<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller
{
	public function Index()
	{
		$this->ViewDVDs();
	}
	
	public function add_dvd()
	{
		$this->AddDVD();
	}
	public function dvds()
	{
		$this->ViewDVDs();
	}
	public function update_dvd()
	{
		$this->UpdateDVD();
	}
	public function delete_dvd()
	{
		$this->DeleteDVD();
	}
	
	public function view($page, $data = array())
	{
		$this->load->model('UserModel');
		$this->load->library(array('cart', 'table'));
		
		$this->load->view("templates/header");
		$this->load->view("templates/sidebars", array('logged_in' => $this->UserModel->isLoggedIn(), 'cart' => $this->cart->contents()));
		$this->load->view('admin/'.$page, $data);
		$this->load->view("templates/footer");
	}
	
	public function ViewDVDs()
	{
		$this->load->library('table');
		$data['query'] = $this->db->get('dvds');
		$this->view('admin_dvds', $data);
	}
	
	public function AddDVD()
	{
		$this->load->helper('form');
		$this->load->library(array('table', 'form_validation'));
		
		$file_uploaded = !empty($_FILES['dvd_cover']['name']);
		
		$this->form_validation->set_message('is_unique', 'This %s already exists.');
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules('dvd_title', 'DVD Title', 'required|max_length[80]|is_unique[dvds.dvd_title]');
		$this->form_validation->set_rules('dvd_price', 'DVD Price', 'required|numeric|greater_than[0]');
		if (!$file_uploaded)
		{
			$this->form_validation->set_rules('dvd_cover', 'DVD Cover Image', 'required');
		}
		
		$config['upload_path'] = dvd_covers_path();
		$config['allowed_types'] = 'gif|jpg|png|bmp';
		$config['max_size']	= '20000';
		$this->load->library('upload', $config);
		
		if(!$this->form_validation->run() || !$this->upload->do_upload('dvd_cover'))
		{
			$this->view('admin_add_dvd');
		}
		else
		{
			$this->load->model('AdminModel');
			$this->AdminModel->addDVD();
			
			$this->view('admin_add_dvd_success');
		}
	}
	
	public function UpdateDVD()
	{
		$this->load->model('AdminModel');
		if(!$this->AdminModel->doesDVDExist($this->uri->segment(3)))
		{
			$this->view('admin_invalid_dvd_id');
			return;
		}
		
		$this->load->helper('form');
		$this->load->library(array('table', 'form_validation'));
		
		$file_uploaded = !empty($_FILES['dvd_cover']['name']);
		
		$this->form_validation->set_error_delimiters('<div class="form_error">', '</div>');
		$this->form_validation->set_rules('dvd_title', 'DVD Title', 'required|max_length[80]');
		$this->form_validation->set_rules('dvd_price', 'DVD Price', 'required|numeric|greater_than[0]');
		
		$config['upload_path'] = dvd_covers_path();
		$config['allowed_types'] = 'gif|jpg|png|bmp';
		$config['max_size']	= '20000';
		$this->load->library('upload', $config);
		
		if(!$this->form_validation->run() || ($file_uploaded && !$this->upload->do_upload('dvd_cover')))
		{
			$this->view('admin_update_dvd');
		}
		else
		{
			$this->AdminModel->updateDVD($this->uri->segment(3), $file_uploaded);
			
			$this->view('admin_update_dvd_success');
		}
	}
	
	public function DeleteDVD()
	{
		$this->load->model('AdminModel');
		if(!$this->AdminModel->doesDVDExist($this->input->post('dvd_id')))
		{
			$this->view('admin_invalid_dvd_id');
		}
		else
		{
			$this->AdminModel->deleteDVD($this->input->post('dvd_id'));
			$this->view('admin_delete_dvd_success');
		}
	}
}